Completed
Pull Request — master (#8)
by Eric
40s
created

cookiejs.js ➔ ... ➔ cookiejs.remove   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 5
rs 9.4285
1
/**
2
 * cookiejs object for setting/getting/removing cookies
3
 */
4
 (function(root, factory) {
5
    if (typeof define === "function" && define.amd) {
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
        define(["cookiejs"], factory);
7
    } else if (typeof module === "object" && module.exports) {
8
        module.exports = factory();
9
    } else {
10
        root.cookiejs = factory();
11
    }
12
}(this, function() {
13
14
    /**
15
     * @type {Object}
16
     */
17
    var cookiejs = {};
18
19
    /**
20
     * sets or overwrites a cookie
21
     * 
22
     * @param {String} sCookieName - the name of the cookie you want to set
23
     * @param {String} sValue - the value you want to set
24
     * @param {String} oAttributes - options e.g. domain, path, expires
25
     *
26
     * @returns {String}
27
     */
28
    cookiejs.set = function(sCookieName, sValue, oAttributes) {
29
        var sAttributes = '';
30
31
        oAttributes = oAttributes || {};
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter oAttributes. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
32
33
        if (typeof oAttributes.path !== 'string') {
34
            sAttributes += '; path=/';
35
        }
36
37
        Object.keys(oAttributes).forEach(function(sAttributeName) {
38
            sAttributes += ';' + sAttributeName + '=' + oAttributes[sAttributeName];
39
        });    
40
41
        document.cookie = encodeURIComponent(sCookieName) + '=' + encodeURIComponent(sValue) + sAttributes;
42
    };
43
44
    /**
45
     * returns the value of a cookie
46
     * 
47
     * @param {String} sCookieName
48
     *
49
     * @returns {String|Boolean}
50
     */
51
    cookiejs.get = function(sCookieName) {
52
        var aCookies,
53
            gCookieValue = false;
54
        
55
        if (!sCookieName || typeof sCookieName != 'string') {
56
            return gCookieValue;
57
        }
58
        
59
        aCookies = document.cookie.split('; ');
60
61
        for (var i = aCookies.length - 1; i >= 0; i--) {
62
            if (decodeURIComponent(aCookies[i].split('=')[0]) === sCookieName) {
63
                gCookieValue = decodeURIComponent(aCookie[1]);
0 ignored issues
show
Bug introduced by
The variable aCookie seems to be never declared. If this is a global, consider adding a /** global: aCookie */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
64
            }
65
        }
66
        
67
        return gCookieValue;
68
    };
69
70
    /**
71
     * removes a specific cookie
72
     *
73
     * oAttributes must contain the correct path and domain else you can't remove the cookie
74
     *
75
     * @param {String} sCookieName
76
     * @param {Object} oAttributes - options e.g. domain, path, expires
77
     */
78
    cookiejs.remove = function(sCookieName, oAttributes) {
79
        oAttributes = oAttributes || {};
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter oAttributes. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
80
        oAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT';
81
        this.set(sCookieName, '', oAttributes);
82
    };
83
84
    return cookiejs;
85
}));